
#ifndef _BILLING40_PROCESS_MGR_H_
#define _BILLING40_PROCESS_MGR_H_

#include "flow.h"
#include "node.h"
#include "nodedef.h"
#include "function_list.h"
#include "config.h"
#include "const.h"
#include <str.h>
#include <lock.h>
#include <map>
#include <list>

NAMESPACE_BILLING40_FRAME_BEGIN

class CThread;
class CControlPkg;

enum RUN_RETRUN_VALUE {
	RET_OK = 0,
	RET_FINISH = 1,
	RET_FATAL = 2
};

class CProcessMgr : public CLogIntf
{
public:
	typedef AISTD map< int32 /*flow id*/, CFlow * > FlowMap;
	enum{
		COMMAND_REQ_APP_PROPERTY = 535,
		COMMAND_RES_APP_PROPERTY = 536,
		COMMAND_REQ_TASK_LIST = 541,
		COMMAND_RES_TASK_LIST = 542,

		COMMAND_CHECK_CONFIG=2,
		COMMAND_UPDATE_CONFIG=3,
		COMMAND_GET_LIB_INFO=4
	};
	enum { 
		MAX_SLEEP_TIME = 2
	};
public:
	int32 start(const char *cszConfigFile, const char *cszProgram, const bool& bSetDaemon);
	int32 stop();
	int32 manage();

	int32 check_config();
	int32 update_config();

	int32 control(CControlPkg& controlPkg);
	int32 send_message(CControlPkg *pControlPkg);

	void set_messageObj(IProcessMessage *pProcessMessage);
	void clear_messageObj(IProcessMessage *pProcessMessage);

	void send_startProcess(int32 iPid, int32 iResult);
	//void send_stopProgress(int32 iProgrcess);

	void LogAppend(const NAMESPACE_BILLING40_LOG LOG_LEVEL& eLevel,
		const char *cszFormat,
		...);
	void LogAppend(NAMESPACE_BILLING40_LOG CLog *pLog,
		const char *cszModuleName,
		const NAMESPACE_BILLING40_LOG LOG_LEVEL& eLevel,
		const char *cszFormat,
		va_list ap);

	CProcessMgr();
	~CProcessMgr();

	CFrameConfig m_objConfig;		//config
	CCommonNodeConfig m_objCommonNodeConfig;

	NAMESPACE_BILLING40_XML_CONFIG CConfig m_objXmlFile;
	NAMESPACE_BILLING40_XML_CONFIG CConfig m_objXmlFile2;
	CFunctionList m_objFunction;	//function list
	CNodeDefMap m_mapNodeDefine;	//node define 
	FlowMap m_mapFlow;		//flow 
	CFlow m_objSysFlow;		//system flow
	CNode m_objClock;		//system clock
	//CNode m_objTimer;		//system timer
	CThread *m_pObjControl;
	IProcessMessage *m_pProcessMessage;
	CThreadMutexLock m_objManageLock;
	CThreadMutexLock m_objMessageLock;

	int32 m_iPid;
	int32 m_iProcId;
	int32 m_iCpfFlowId;
	int32 m_iCpfServiceId;
	int32 m_iCpfProcId;

	static int32 m_iSignal;
	static int32 m_iRet;
private:
	CProcessMgr(const CProcessMgr& rh);
	CProcessMgr& operator=(const CProcessMgr& rh);
	int32 check_process(const char *cszProgram);
	void remove_pidFile();
	int32 check_dir(const char *cszDir);
//	int32 mkdir(char *szDir) const;
	int32 redirect_stdio();
	int32 set_daemon();

	int32 m_iPipe[2];
	time_t m_lLastManageTime;
	int32 m_iSendLogLevel;
	pthread_t m_tid;

	time_t m_tSleepTime;
};

//int32 control(const CControlPkg& controlPkg);

NAMESPACE_BILLING40_FRAME_END

extern NAMESPACE_BILLING40_FRAME CProcessMgr *g_pProcessMgr;

#endif //_BILLING40_PROCESS_MGR_H_

